package ag.script.project.standard.core;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;

import java.io.File;
import java.util.List;

public class ScriptProjectList {
    private final String projectPath;//项目路径
    private final ScriptProjectStandardLog log;

    public ScriptProjectList(String projectPath) {
        this.projectPath = projectPath;
        this.log = new ScriptProjectStandardLog(projectPath);
    }


    public void start() {
        if (!FileUtil.exist(projectPath)) {
            throw new RuntimeException(" projectPath is not exits!");
        }
        if (!FileUtil.isDirectory(projectPath)) {
            throw new RuntimeException(" projectPath is must a directory!");
        }
        log.log("【生成list清单文件】");
        ScriptProject scriptProject = new ScriptProject(projectPath, "", "");
        String projectName = scriptProject.getProjectName();
        File fileRes = scriptProject.getFile_res();
        File fileList = scriptProject.getFile_list();
        FileUtil.writeUtf8String("", fileList);
        List<File> filesRes = FileUtil.loopFiles(fileRes, pathname -> !StrUtil.equals(fileList.getAbsolutePath(), pathname.getAbsolutePath()));
        for (File file : filesRes) {
            String shortPath = StrUtil.subAfter(file.getAbsolutePath(), projectName, false);
            String shortPath_handle = StrUtil.addPrefixIfNot(StrUtil.replace(StrUtil.replace(shortPath, "\\\\", "/"), "\\", "/"), "/");
            String androidPath = "/sdcard/Pictures/" + projectName + shortPath_handle;
            FileUtil.appendUtf8String(androidPath + "\n", fileList);
            log.log("安卓路径:" + androidPath, "\n电脑路径:" + file.getAbsolutePath());
        }
    }


}
